package org.example.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tangtl
 * @create 2021-9-3 10:34
 */
@Configuration
public class RedissonSpringDataConfig {
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;


    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient(){
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer().setAddress("redis://" + host + ":" + port);
        if(StringUtils.isNotBlank(password)){
            System.out.println("------------- password -----------------------");
            singleServerConfig.setPassword(password);
        }
        System.out.println("------------- redisson -----------------------");
        System.out.println(config.getTransportMode());;
        //TransportMode.EPOLL 只能在linux下使用,原则上应该比默认的nio快
        /*
        transportMode
Default value: TransportMode.NIO
Available values:
TransportMode.NIO,
TransportMode.EPOLL - requires netty-transport-native-epoll lib in classpath
TransportMode.KQUEUE - requires netty-transport-native-kqueue lib in classpath
         */
//        config.setTransportMode(TransportMode.EPOLL);
        return Redisson.create(config);
    }

}